/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.ChiTietDonDatHang;
import pojo.SanPham;
import pojo.ThamSo;
import service.ChiTietDonDatHangService;
import service.SanPhamService;
import service.ThamSoService;

/**
 *
 * @author MongKieu
 */
public class TimKiemSanPhamController extends AbstractController {

    private SanPhamService sanPhamService;
    private ChiTietDonDatHangService chiTietDonDatHangService;
    private ThamSoService thamSoService;

    public ThamSoService getThamSoService() {
        return thamSoService;
    }

    public void setThamSoService(ThamSoService thamSoService) {
        this.thamSoService = thamSoService;
    }

    public ChiTietDonDatHangService getChiTietDonDatHangService() {
        return chiTietDonDatHangService;
    }

    public void setChiTietDonDatHangService(ChiTietDonDatHangService chiTietDonDatHangService) {
        this.chiTietDonDatHangService = chiTietDonDatHangService;
    }

    public SanPhamService getSanPhamService() {
        return sanPhamService;
    }

    public void setSanPhamService(SanPhamService sanPhamService) {
        this.sanPhamService = sanPhamService;
    }

    public TimKiemSanPhamController() {
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView("TimKiemSanPham");
        try {
            ThamSo thamSo = thamSoService.getThamSoByMaThamSo(1);
            ThamSo thamSoSPToiDa = thamSoService.getThamSoByMaThamSo(2);
            //Tim KIem thong tin san pham
            String theLoai = request.getParameter("TheLoai");
            String nhomSanPham = request.getParameter("MaNhomSanPham");
            int soTrang = 0;
            int trangHienTai = 1;

            if (request.getParameter("Trang") != null) {
                trangHienTai = Integer.parseInt(request.getParameter("Trang"));
            }
            if (theLoai != null && nhomSanPham != null) {

                List<SanPham> listSanPham;
                soTrang = sanPhamService.tinhSoLuongTrang(Integer.parseInt(theLoai));
                listSanPham = sanPhamService.listSanPhamTheoTheLoaiCoPhanTrang(Integer.parseInt(theLoai), trangHienTai);
                mv.addObject("listSanPham", listSanPham);
                mv.addObject("soTrang", soTrang);
                mv.addObject("theLoai", theLoai);
                mv.addObject("nhomSanPham", nhomSanPham);
                mv.addObject("soLuongSanPham", sanPhamService.soLuongSanPhamTheoTheLoai(Integer.parseInt(theLoai)));
                mv.addObject("trangHienTai", trangHienTai);
                mv.addObject("bookKind", 0);
            } else if (request.getParameter("bookKind") != null) {
                int bookKind = Integer.parseInt(request.getParameter("bookKind"));
                if (bookKind == 1) {
                    // Set so luongSanPhamMAx la 100
                    int soLuongSanPhamMax = 100;
                    List<SanPham> listSanPham = new ArrayList<SanPham>();
                    List<ChiTietDonDatHang> listChiTietDonDatHang = chiTietDonDatHangService.listChiTietDonDatHangBanChayNhatCoPhanTrang(trangHienTai);
                    for (int i = 0; i < listChiTietDonDatHang.size(); i++) {
                        ChiTietDonDatHang ct = listChiTietDonDatHang.get(i);
                        listSanPham.add(ct.getSanPham());
                    }
                    mv.addObject("listSanPham", listSanPham);
                    soTrang = chiTietDonDatHangService.tinhSoLuongTrang(soLuongSanPhamMax);
                    mv.addObject("soTrang", soTrang);
                    mv.addObject("soLuongSanPham", chiTietDonDatHangService.soLuongChiTietDonHangBanChayNhat(soLuongSanPhamMax));
                    mv.addObject("trangHienTai", trangHienTai);
                    mv.addObject("bookKind", 1);
                } else if (bookKind == 2) {
                    List<SanPham> listSanPham;
                    soTrang = sanPhamService.tinhSoLuongTrangChoSanPhamMoiNhapVe(thamSo.getSoLuongSanPham(), thamSoSPToiDa.getSoLuongSanPham(), "Moi");
                    listSanPham = sanPhamService.listSanPhamMoiNhapVeCoPhanTrang(trangHienTai, thamSo.getSoLuongSanPham(), "Moi");
                    mv.addObject("listSanPham", listSanPham);//
                    mv.addObject("soTrang", soTrang);//

                    mv.addObject("soLuongSanPham", sanPhamService.soLuongSanPhamMoiNhapVe(thamSoSPToiDa.getSoLuongSanPham(), "Moi"));
                    mv.addObject("trangHienTai", trangHienTai);
                    mv.addObject("bookKind", 2);
                } else if (bookKind == 3) {
                    List<SanPham> listSanPham;
                    soTrang = sanPhamService.tinhSoLuongTrangChoSanPhamMoiNhapVe(thamSo.getSoLuongSanPham(), thamSoSPToiDa.getSoLuongSanPham(), "SapPhatHanh");
                    listSanPham = sanPhamService.listSanPhamMoiNhapVeCoPhanTrang(trangHienTai, thamSo.getSoLuongSanPham(), "SapPhatHanh");
                    mv.addObject("listSanPham", listSanPham);//
                    mv.addObject("soTrang", soTrang);//
                    mv.addObject("soLuongSanPham", sanPhamService.soLuongSanPhamMoiNhapVe(thamSoSPToiDa.getSoLuongSanPham(), "SapPhatHanh"));
                    mv.addObject("trangHienTai", trangHienTai);
                    mv.addObject("bookKind", 3);
                }
            } else if (request.getParameter("EventID") != null && request.getParameter("SearchText") != null && request.getParameter("maNhomSearch") != null) {
                String timKiemChinh = request.getParameter("EventID");
                String searchText = request.getParameter("SearchText");
                String nhomSanPhamID = request.getParameter("maNhomSearch");
                if (timKiemChinh.equals("TimKiemChinh")) {
                    List<SanPham> listSanPham;
                    soTrang = sanPhamService.tinhSoLuongTrangChoSanPhamTimKiemTheoTenVaNhomSanPham(searchText, Integer.parseInt(nhomSanPhamID), thamSo.getSoLuongSanPham());
                    listSanPham = sanPhamService.listSanPhamTheoTenVaNhomSanPhamCoPhanTrang(searchText, Integer.parseInt(nhomSanPhamID), trangHienTai, thamSo.getSoLuongSanPham());
                    mv.addObject("listSanPham", listSanPham);
                    mv.addObject("soTrang", soTrang);
                    mv.addObject("NhomSanPhamID", nhomSanPhamID);
                    mv.addObject("SearchText", searchText);
                    mv.addObject("EventID", request.getParameter("EventID"));
                    int soLuong = sanPhamService.soLuongSanPhamimKiemTheoTenVaNhomSanPham(searchText, Integer.parseInt(nhomSanPhamID));
                    mv.addObject("soLuongSanPham", soLuong);
                    mv.addObject("trangHienTai", trangHienTai);
                    mv.addObject("bookKind", 4);
                }
            }
        } catch (Exception ex) {
            System.out.println("Xay ra loi");
            mv = new ModelAndView("ThongBaoLoi");
            mv.addObject("messageError", "Xin chào bạn, thông tin tìm kiếm không hợp lệ.");
        }
        return mv;
    }
}
